import { Injectable } from '../decorators/inject';
import { Config } from './config';
import { TableDatas } from './table-datas';
import { DrawHelper } from '../utils/draw-helper';
import { Inject } from '../decorators/inject';
import { NExcel } from '../types';
@Injectable('FreezeRender')
export class FreezeRender {
    protected ctx: CanvasRenderingContext2D;
    protected drawRowLine: (y: number) => void = () => {};
    protected drawColLine: (x: number) => void = () => {}
    constructor(
        protected conf: Config,
        protected tableDatas: TableDatas,
        @Inject(NExcel.TableFreezeCanvasToken) protected canvasEle: HTMLCanvasElement,
    ) {

    }
    public init() {
        this.ctx = this.canvasEle.getContext('2d');
        let draw = DrawHelper.line(this.ctx)({width: 3, type: 'solid', color: 'rgba(170, 170, 170,0.6)'});
        this.drawRowLine = (() => {
            return (y: number) => {
                const canvasW = Number(this.canvasEle.style.width.replace('px', ''));
                const width = canvasW * this.conf.pixelRatio;
                draw({x: 0, y: y}, {x: width, y: y});
            }
        })();
        this.drawColLine = (() => {
            return (x: number) => {
                const canvasH = Number(this.canvasEle.style.width.replace('px', ''));
                const height = canvasH * this.conf.pixelRatio;
                draw({x: x, y: 0}, {x: x, y: height});
            }
        })();
    }
    public getStartRowHeight() {
        let height = 0;
        let freezeRow = this.conf.freeze.row;
        for(let i = 0; i < freezeRow + 1; i++) {
            height +=  this.tableDatas.getRowHeight(i);
        }
        return height;
    }
    public getStartColWidth() {
        let width = 0;
        let freezeCol = this.conf.freeze.col;
        for(let i = 0; i < freezeCol + 1; i++) {
            width += this.tableDatas.getColWidth(i);
        }
        return width;
    }
    protected renderSpliteLine() {
        if(this.conf.freeze.row !== -1) {
            let height = this.getStartRowHeight() + this.conf.header.col.height - 1 * this.conf.pixelRatio - 1;
            this.drawRowLine(height);
            // this.drawRowLine(height);
        }
        if(this.conf.freeze.col !== -1) {
            let width = this.getStartColWidth() + this.conf.header.row.width - this.conf.pixelRatio - 1;
            this.drawColLine(width);
        }
    }
    
    public render() {
        const canvasW = this.canvasEle.clientWidth * this.conf.pixelRatio;
        const canvasH = this.canvasEle.clientHeight * this.conf.pixelRatio;
        this.ctx.clearRect(0, 0, canvasW + 1, canvasH + 1)
        this.renderSpliteLine();
    }
}